<?php

namespace Admin\Controller;

use Common\Library\Util\Tree;

/** Description:	权限管理
 * @Author:Tudo<997470395@qq.com>
 * @CreateTime:2015-2-1 下午2:49:51 */
class RbcController extends BaseController
{
	private $msg = array('url' => null,'success' => '添加成功!','error' => '添加失败!');
	
	/** 配置权限 */
	public function setRole() {
		$m = M( 'rbac_node' );
		if (IS_POST) {
			$rid = I( 'post.rid', 0, "intval" );
			$nodes = I( 'post.node', '', 'trim' );
			if ($rid <= 0 || empty( $nodes )) {
				die( json_encode( array('status' => - 1,'msg' => '参数异常！') ) );
			}
			
			$map = array();
			if (strpos( $nodes, ',' )) {
				
				$nodes = explode( ",", $nodes );
				foreach ( $nodes as $k => $v ) {
					$map[] = array('role_id' => $rid,'node_id' => $v);
				}
			} else {
				$map[] = array('role_id' => $rid,'node_id' => $nodes);
			}
			$aModel = M( 'rbac_access' );
			if ($aModel->where( array('role_id' => $rid) )->count() > 0) {
				$aModel->where( array('role_id' => $rid) )->delete();
			}
			
			if ($aModel->addAll( $map )) {
				die( json_encode( array('status' => 1,'msg' => '授权成功！') ) );
			} else {
				die( json_encode( array('status' => - 1,'msg' => '授权失败！') ) );
			}
		} else {
			$rid = I( 'get.id', 0, 'intval' );
			if ($rid <= 0) {
				$this->error( "请传递角色Id" );
				exit();
			}
			
			$list = $m->order( 'sort' )->select();
			$list = Tree::unlimitedForLevel( $list );
			$rbc = array(); // 存放最新的权限
			
			$accModel = M( "access", "rbac_" );
			// 这不是一个高效的方法，但是是一个简单的方法！
			foreach ( $list as $k => $v ) {
				$count = $accModel->where( array('role_id' => $rid,'node_id' => $v['id']) )->count();
				if ($count > 0) {
					$list[$k]['access'] = 1;
				} else {
					$list[$k]['access'] = 0;
				}
			}
	//	 print_r($list); 
			 
			 //exit();
			
			$rName = M( 'role', 'rbac_' )->getFieldById( $rid, 'name' );
			$this->assign( "list", $list );
			$this->assign( "rid", $rid );
			$this->assign( "rname", $rName );
			$this->display();
		}
	}
	
	/** 权限列表 * */
	public function node() {
		$list = M( 'rbac_node' )->order( 'sort' )->select();
		$list = Tree::unlimitedForLevel( $list );
		// print_r($list);
		$this->assign( "list", $list );
		$this->display();
	}
	
	/** 添加权限* */
	public function addNode() {
		if (IS_POST) {
			$data = I( 'post.' );
			$this->msg['url'] = U( 'rbc/node' );
			// 暂时没有验证
			$this->addRow( "rbac_node", $data, $this->msg );
		} else {
			$pnode = $this->lists( "rbac_node", array('level' => array('neq',3)), "sort" );
			$this->assign( "pnode", $pnode );
			$this->display();
		}
	}
	
	/** 删除权限 */
	public function delNode() {
		$id = I( 'get.id' );
		$this->msg['url'] = U( 'rbc/node' );
		$this->msg['success'] = "删除成功！";
		$this->msg['error'] = "删除失败！";
		
		$this->delTrue( "rbac_node", array('id' => $id), $this->msg );
	}
	
	/** 添加角色 * */
	public function addRole() {
		if (IS_POST) {
			// 暂时先不验证
			$data = I( 'post.' );
			$this->msg['url'] = U( 'rbc/Role' );
			$this->addRow( "rbac_role", $data, $this->msg );
		} else {
			$this->display();
		}
	}
	
	/** 删除角色 * */
	public function delRole() {
		$id = I( 'get.id' );
		$this->msg['url'] = U( 'rbc/role' );
		$this->msg['success'] = "删除成功！";
		$this->msg['error'] = "删除失败！";
		$this->delTrue( "rbac_role", array('id' => $id), $this->msg );
	}
	
	/** 角色 */
	public function Role() {
		$list = $this->lists( "rbac_role" );
		$this->assign( "list", $list );
		$this->display();
	}
}